﻿using Microsoft.Phone.Tasks;
using Q42.WinRT.Data;
using SpaceNeedle.SDK.Windows.Platform.Commands;
using SpaceNeedle.SDK.Windows.Platform.Data;
using SpaceNeedle.SDK.Windows.Platform.Services.Location;
using SpaceNeedle.SDK.WindowsPhone;
using SpaceNeedle.x6473.WindowsPhone.Common;
using SpaceNeedle.x6473.WindowsPhone.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace SpaceNeedle.x6473.WindowsPhone.ViewModels
{
    public class RequestViewModel : GlobalViewModel
    {
        public RequestViewModel() :base()
        {
            this.IsAutoLoadEnabled = false;
            this.RequestItem = new RequestItem() { Id = Guid.NewGuid() };
        }

        public Uri MapAddress
        {
            get { return this.Get<Uri>(); }
            set { this.Set(value); }
        }

        public Geopos CurrentPosition
        {
            get { return this.Get<Geopos>(); }
            set { this.Set(value); }
        }

        public Address CurrentAddress
        {
            get { return this.Get<Address>(); }
            set { this.Set(value); }
        }

        public AppContent.RequestInfo StaticContent
        {
            get { return this.Get<AppContent.RequestInfo>(); }
            set { this.Set(value); }
        }

        public bool IsShareVisible
        {
            get { return this.Get<bool>(); }
            set { this.Set(value); }
        }

        public string UserTypedAddress
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string UserTypedDescription
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string PageTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string CallTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }
        
        public string DescriptionTitle
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public string ShareText
        {
            get { return this.Get<string>(); }
            set { this.Set(value); }
        }

        public Models.Request Request
        {
            get { return this.Get<Models.Request>(); }
            set { this.Set(value); }
        }

        public Models.RequestItem RequestItem
        {
            get { return this.Get<Models.RequestItem>(); }
            set { this.Set(value); }
        }

        protected async override void Load()
        {
            this.IsInProgress = true;

            await base.LoadAsync();

            this.StaticContent = this.AppContent.Request;

            this.Request = AppViewModel.Instance.Properties.SelectedRequest;

            this.RequestItem.RequestId = AppViewModel.Instance.Properties.SelectedRequest.Id;
            this.RequestItem.CreatedBy = AppViewModel.Instance.Properties.Me.Id.ToString();

            await UpdateLocationInfoAsync();
            await AppServices.Current.ScheduleService.ScheduleAction(TimeSpan.FromSeconds(5), false, TimeSpan.Zero, new Action(UpdateLocationInfo));
            
            this.IsShareVisible = AppViewModel.Instance.IsAuthenticated && !string.IsNullOrEmpty(AppViewModel.Instance.Properties.SocialNetworkName);
            this.ShareText = "Compartilhar no " + AppViewModel.Instance.Properties.SocialNetworkName;

            Tracking.SendView(ViewsNames.Request);

            this.IsInProgress = false;
        }

        private async Task UpdateLocationInfoAsync()
        {
            var currentPosition = await AppServices.Current.LocationService.GetGeopositionAsync();

            if(currentPosition == null)
            {
                return;
            }

            this.CurrentPosition = currentPosition;

            this.RequestItem.Geoposition = this.CurrentPosition.Coordinate.Latitude.Value.ToString().Replace(",", ".") + "," + this.CurrentPosition.Coordinate.Longitude.Value.ToString().Replace(",", ".");

            this.MapAddress = new Uri(string.Format("http://dev.virtualearth.net/REST/v1/Imagery/Map/Road/{0}/12?mapSize=440,160&pushpin={0};68&mapLayer=mapLayer&format=png&key={1}",
                this.RequestItem.Geoposition,
                Properties.BingMapsKey), UriKind.Absolute);

            var currentAddress = await AppServices.Current.LocationService.GetAddressAsync(currentPosition);

            if(currentAddress == null || string.IsNullOrEmpty(currentAddress.FormattedAddress))
            {
                return;
            }

            this.CurrentAddress = currentAddress;
            

            this.RequestItem.Address = this.CurrentAddress.FormattedAddress;

            if (string.IsNullOrEmpty(this.UserTypedAddress))
            {
                this.UserTypedAddress = this.CurrentAddress.FormattedAddress;
            }
        }

        private async void UpdateLocationInfo()
        {
            await UpdateLocationInfoAsync();
        }
    }
}
